package com.acc.dao.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.acc.dao.UserDao;
import com.acc.entity.User;
import com.acc.utils.JDBCTools;
/**
 * @author 姜伟康
 *2017-9-15
 */
public class UserDaoImpl extends JDBCTools implements UserDao {

	
	
	/**
	 * 根据用户ID查询用户
	 * @param orderId
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public User selectUserById(Integer userId) {
		// TODO Auto-generated method stub

		User user = null ;
		
		// 获取连接
		Connection conn = null;
		// 查询语句
		// 创建PreparedStatement
		PreparedStatement ps = null;
		// 获取结果集
		ResultSet rs = null;
		// 查询语句
		String sql = "select * from TI_USER where user_id=?";

		try {
			conn = super.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setIdcard(rs.getString("idCard"));
				user.setPassword(rs.getString("password"));
				user.setRegisterDate(rs.getDate("register_date"));
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(rs.getString("user_name" ));
				user.setGender(rs.getInt("gender" ));
				user.setType(rs.getInt("type" ));
			}
			return user;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭连接
			super.close(conn, ps, rs);
		}
		
		return null;
	}

	
	
	
	
	
	/**
	 * 修改用户信息
	 * @param user 用户信息
	 * @throws IOException
	 * @throws SQLException
	 */
	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		// 获取连接
		Connection conn = null;
		// 创建PreparedStatement
		PreparedStatement ps = null;
		// 创建修改语句
		String sql = "update TI_USER set user_name=?,password=?,idCard=?,gender=?,type=? where user_id=?";
		
		try {
			conn = super.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1,  user.getUserName());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getIdcard());
			ps.setInt(4, user.getGender());
			ps.setInt(5, user.getType());
			ps.setInt(6, user.getUserId());
			
			// 执行修改
		    int result = ps.executeUpdate();
		    
		    return result;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//关闭连接
			super.close(conn, ps, null);
		}
		return 0;
		
	}

}
